<?php 
namespace Admin\Controller;
use Think\Controller;
header('Access-Control-Allow-Origin:*');
header('Acess-Control-Allow-Methods:POST,GET');
class MusicController extends Controller{
		protected $db;
		protected function _initialize(){
			$this->db = D('music');
		}
		
		//异步接口
		public function ajax(){
			if(I('get.id')){
				$data = $this->db->getJoin(I('get.id'));
				foreach($data as $i=> $v){
					foreach($v as $j=>$k){
						if($j=='authorimg'){
							$arr[$i][$j] = json_decode($k);
						}else{
							$arr[$i][$j]=$k;
						}
						
					}
				}
				$this->ajaxReturn($arr);
			}else{
				$data = $this->db->getJoin();
				foreach($data as $i=> $v){
					foreach($v as $j=>$k){
						if($j=='authorimg'){
							$arr[$i][$j] = json_decode($k);
						}else{
							$arr[$i][$j]=$k;
						}
						
					}
				}
				$this->ajaxReturn($arr);
			}	
		}
		
		public function index(){
			$data = $this->db->getJoin();		
 			
			$this->assign('data',$data);
			$this->display();
		}
		
		public function music(){
			if(IS_GET){
				$id = I('get.id');
				$data = $this->db->getAll($id);
				$this->assign('id',$id);
				$this->assign('data',$data);
				$this->display();
			}
			if(IS_POST){
				if($this->db->music()){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}
		}
		
		public function del(){
			if($this->db->del()){
				$this->success('删除成功');
			}else{
				$this->error($this->db->getError());
			}
		}
		
		public function edit(){
			if(IS_GET){
				$id = I('get.id');
				$data = $this->db->getJoin($id);
 
				$sorts = $this->db->get('sorts');
				$author = $this->db->get('author');
				$share = $this->db->get('share');
 
				$this->assign('share',$share);
				$this->assign('sorts',$sorts);
				$this->assign('author',$author);
				$this->assign('data',$data[0]);
				$this->assign('id',$id);
				$this->display();
			}
			
 
			

			if(IS_POST){	 
				if($this->db->edit()){
					$this->success('修改成功');
				}else{
					if(empty($this->db->getError())){
						$this->error('修改失败，值相同');
					}else{
						$this->error($this->db->getError());
					}
					
				}
			}
			
			
		}
		
		public function add(){
			if(IS_POST){
				if($this->db->m_add()){
					$this->success('添加成功');
				}else{
					$this->error($this->db->getError());
				}
				
				
				
			}else{			
				$sorts = $this->db->get('sorts');
				$author = $this->db->get('author');
				$share = $this->db->get('share');
 
				$this->assign('share',$share);
				$this->assign('sorts',$sorts);
				$this->assign('author',$author);
				$this->display();
			}
		}
}